<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/23
 * Time: 10:26
 */
namespace app\blog\model;
use think\Model;
use think\Db;
class DekeyModl extends Model
{
    /**
     * 获取激活码记录
     */
    public function getAll($param){
        $id = $param['id'];
        $where = array();
        if(!empty($param['key_code'])){
            $where['d.key_code'] = $param['key_code'];
        }
        $result = Db::name('dekey')->alias('d')
                                        ->join('__AGENCY__ a','d.user_id=a.user_id')
                                        ->where(['d.user_id'=>$id])
                                        ->where($where)
                                        ->field('a.brand_name,d.id,d.key_code,d.status,d.make_id')
                                        ->paginate(10);
        if($result->toArray())
        {
            $result->toArray();
            foreach($result as $k=>$v){
                $data = array();
                $data = $v;
                //判断使用者是否存在
                $make_id = $v['make_id'];
                if($make_id != ''){
                    $rests = Db::name('user')->where(['id'=>$make_id])->find();
                    $data['make_mobile'] = $rests['mobile'];
                }else{
                    $data['make_mobile'] = '--';
                }
                $result->offsetSet($k,$data);
            }
        }
        return $result;
    }
    /**
     * 购买添加激活码
    */
    public function buyKey($param){
        $id      = $param['id'];
        $number  = $param['number'];
        $money   = $param['money'];
        $arra    = array();
        for($i=0;$i<$number;$i++){
            $arra['dk_guid']     = guid();//唯一标识
            $arra['key_code']    = guidz();//激活码
            $arra['user_id']     = $id;
            $arra['status']      = 2;
            $arra['c_time']      = time();
            $rest = Db::name('dekey')->insert($arra);
        }
        //添加资金记录
        $date    = array();
        $date['b_guid']      = guid();//唯一标识
        $date['money']       = $money;
        $date['agency_id']   = $id;
        $date['status']      = 2;
        $date['c_time']      = time();
        $rest1 = Db::name('balance')->insert($date);
        return $rest;
    }
}